package org.jground.rx.ui.zk.viewControl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RX;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXTypeComponent;
import org.jground.rx.RXView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIViewPanel;
import org.jground.util.ArrayUtils;
import org.jground.zk.zulX.DivX;
import org.jground.zk.zulX.HboxX;
import org.zkoss.zhtml.Hr;
import org.zkoss.zul.Label;

public class UIAttributeViewComponent extends UIAttributeViewBase {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(UIAttributeViewComponent.class);

    public UIAttributeViewComponent createNew(UIFrame uiFrame, RXView rxControl) {
        return new UIAttributeViewComponent(uiFrame, rxControl);
    }

    //
    public UIAttributeViewComponent() {}

    private String[] oids;
    private String lookupMoid;
    private List<UIViewPanel> compViewPanels = new ArrayList<UIViewPanel>();
    private boolean numbering;
    private int currentNumberOfComponents;
    private DivX div;

    private UIAttributeViewComponent(UIFrame uiFrame, RXView rxControl) {
        super(uiFrame, rxControl);
        logger.debug("init UIAttributeViewComp");
        //
        RXTypeComponent rxType = (RXTypeComponent) rxControl.getRXAttribute().getRXType().getBaseType();
        lookupMoid = rxType.getLookupMoid();
        numbering = rxControl.hasProperty(RX.AttValue.numbering);
        div = new DivX();
        div.setStyle("width:100%;border: green 0px solid");
        add(div);
    }

    public void set(String... values) {
        clear();
        if (ArrayUtils.isNotEmpty(values)) {
            oids = values;
            updateUI();
        }
    }

    private void updateUI() {
        List<IRXObject> comps = RXObjectService.getInstance().getByIds(oids);
        currentNumberOfComponents = comps.size();
        int index = 0;
        div.removeAll();
        
        for (IRXObject mod : comps) {
            if (compViewPanels.size() < currentNumberOfComponents) {
            	
            	
                compViewPanels.add(new UIViewPanel(uiFrame, lookupMoid));
            }
            UIViewPanel uiViewPanel = compViewPanels.get(index);
            uiViewPanel.setData(mod);
            if (numbering) {
                div.add(new HboxX(new Label(String.valueOf(index + 1)), uiViewPanel.getView()));
            } else {
            	if (index !=0){
            		Hr hr = new Hr();
            		hr.setStyle("height:1px;border:none;border-top:1px solid gray");
            		div.add(hr);
            	}
                div.add(uiViewPanel.getView());
            }
            index++;
        }
    }

    @Override
    public Collection<String> renderToString() {
        if (ArrayUtils.isNotEmpty(oids)) {
            List<IRXObject> comps = RXObjectService.getInstance().getByIds(oids);
            for (int index = 0; index < comps.size(); index++) {
                renderToStringS.add(uiFrame.getUISynopsisRendererFactory().toString(comps.get(index)));
            }
        }
        return renderToStringS;
    }

    @Override
    public void clear() {
        renderToStringS.clear();
        oids = null;
        updateUI();
    }

}
